/*!
 * \file frservice_get.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "module.h"
#include "frreport_comm.h"
#include "frkyro.h"

#include "frct_expect.h"
#include "frservice_comm.h"

#include "apmtest_comm.h"

int frkyro_read_utf8(Frbyte_Packet *p_st_data
                      , uint8_t u8_first_char
                      , char **pp_ascii_value);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\xe6\xb5\x8b\xe8\xaf\x95\x64\x64\x64", 9);

    i_ret = frkyro_read_utf8(p_st_data, 0x86, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_ascii_value, (char *)"\xe6\xb5\x8b\xe8\xaf\x95\x64\x64\x64");

    frfree (p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

static void __case2(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x64\x64\x64\xe6\xb5\x8b\xe8\xaf\x95", 9);

    i_ret = frkyro_read_utf8(p_st_data, 0x86, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_ascii_value, (char *)"\x64\x64\x64\xe6\xb5\x8b\xe8\xaf\x95");

    frfree (p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x64\x64\x64\xe6\xb5\x8b\xe8\xaf\x95\x64", 10);

    i_ret = frkyro_read_utf8(p_st_data, 0x86, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_ascii_value, (char *)"\x64\x64\x64\xe6\xb5\x8b\xe8\xaf\x95");

    frfree (p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x64\x64\x64\xe6\xb5\x8b\xe8\xaf", 8);

    i_ret = frkyro_read_utf8(p_st_data, 0x86, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree (p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\xe6\xb5\x8b\xe8\xaf\x95\x64", 7);

    i_ret = frkyro_read_utf8(p_st_data, 0x83, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_ascii_value, (char *)"\xe6\xb5\x8b\xe8\xaf\x95");

    frfree(p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

static void __case6(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\xe6\xb5\x8b\xe8\xaf\x95\x64", 7);

    i_ret = frkyro_read_utf8(p_st_data, 0x80, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_NULL(p_ascii_value);

    bytebuff_free(&p_st_data);
    return;
}

static void __case7(void)
{
    int i_ret = FR_SUCC;
    char *p_ascii_value = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\xe6\xb5\x8b\xe8\xaf\x95\x64", 7);

    i_ret = frkyro_read_utf8(p_st_data, 0x81, &p_ascii_value);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_ascii_value, (char *)"");

    frfree (p_ascii_value);
    bytebuff_free(&p_st_data);
    return;
}

int add_suite_frkyro_read_utf8(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frkyro frkyro_read_utf8()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);

    return FR_SUCC;
}
